package threadLearn;

public class demon2 {
    public static void main(String[] args) throws InterruptedException {
        Thread thread1=new Thread(()->{
            for (int i = 0; i < 4; i++) {
                System.out.println("hello 线程thread1");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        Thread thread2=new Thread(()->{
            for (int i = 0; i < 3; i++) {
                System.out.println("线程thread2");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        thread1.start();
        thread2.start();
        System.out.println("主线程开始等待");
        thread2.join();
        thread1.join();

       // thread1.join();

        //关于两个线程等待先后的问题,因为主线程是等待这两个线程结束,
        //这两个阻塞方法会被同时调用,首先如果没有sleep方法,哪个线程调用join方法
        //先执行完哪个线程,如果有sleep方法,首先会调用线程2,然后线程2,睡1秒,
        //线程2进入阻塞状态,这时会执行下面的代码,让线程1,优先被执行,然后线程1,同理也是这样
        //两个线程会交替执行在前三秒,之后执行时间少的,执行完join方法就不再生效.
        System.out.println("主线程等待结束");
    }
}
